package mgorm

import (
	"gitee.com/dreamwood/jxgo/v1/ss"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

type MongoDbConfig struct {
	User     string   `yaml:"user"`
	Pass     string   `yaml:"pass"`
	Hosts    []string `yaml:"hosts"`
	Auth     string   `yaml:"auth"`
	Db       string   `yaml:"db"`
	LogOn    bool     `yaml:"log-on"`
	LogTo    string   `yaml:"log-to"`
	PoolSize uint64   `yaml:"pool-size"`
}

type Doc interface {
	DocGetName() string
	DocGetId() primitive.ObjectID
	DocSetId(id string)
	DocGetFactory() *Factory
}

//type JxDoc struct {
//	Factory *Factory
//}
//
//func (this *JxDoc) SetEvent(isOn bool) *JxDoc {
//	this.GetFactory().IsEventOn = isOn
//	return this
//}
//
//func (this *JxDoc) GetFactory() *Factory {
//	if this.Factory == nil {
//		this.Factory = NewFactory()
//	}
//	return this.Factory
//}

type Query struct {
	Filters       map[string]interface{} `json:"_filters"`
	Order         []string               `json:"_orders"`
	Page          int                    `json:"page"`
	Size          int                    `json:"size"`
	Depth         int                    `json:"_depth"`
	Dumps         []string               `json:"_dumps"`
	IncludeDelete bool                   `json:"_inc_del"`
	//Group         string                 `json:"_group"`
}

func NewQuery() *Query {
	return &Query{
		Filters: make(map[string]interface{}),
		Order:   make([]string, 0),
		Dumps:   make([]string, 0),
		Page:    1,
		Size:    10,
	}
}

type DocIds struct {
	Ids []int64 `json:"ids"`
}
type SimpleDoc struct {
	Id primitive.ObjectID `jx:"id" json:"id" bson:"_id,omitempty"`
}

func NewIds() *DocIds {
	return &DocIds{make([]int64, 0)}
}

type DocUpdater struct {
	Id    int64   `json:"id"`
	Ids   []int64 `json:"ids"`
	Model ss.Obj  `json:"model"`
}

func NewDocUpdater() *DocUpdater {
	return &DocUpdater{}
}

type Counter struct {
	Count int64 `bson:"count"`
}

type CounterFloat struct {
	Count float64 `bson:"count"`
}

type DBContainer map[string]*mongo.Database

func (ds DBContainer) SetDb(name string, db *mongo.Database) {
	ds[name] = db
}
func (ds DBContainer) GetDb(name string) *mongo.Database {
	if db, ok := ds[name]; ok {
		return db
	}
	return nil
}
